<?php

// @formatter:off
/**
 * @file service.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: service.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/service.php $
 */
// @formatter:on

define("__SERVICE__", true);
header('Access-Control-Allow-Origin: *');

require_once __DIR__."/config/config.php";

$wcDefaults["errors"]["errors"] = false;
$wcDefaults["errors"]["warnings"] = false;
$wcDefaults["errors"]["dies"] = false;

WCService::ActivateDebugs();
if(!defined("WC_NOT_INSTALLED")) {
	$timer = WCTimer::Instance();
	$timer->startGlobal();

	require_once "{$wcPaths["includes-directory"]}/dynamic.conf.php";
	require_once "{$wcPaths["includes-directory"]}/tools.php";

	$cacheId = "SERVICE";
	//
	// Creating a service to manage current request.
	$wcServiceManager = WCServicesManager::I();
	$wcService = $wcServiceManager->currentService();
	if(isset($_REQUEST["explainsearch"])) {
		$wcServiceManager->promptSearch();
	} elseif($wcService) {
		//
		// Checking and running service.
		if(isset($_REQUEST["explaininterface"])) {
			if(!$wcService instanceof WCServiceDummy) {
				$wcService->promptInterface();
			}
		} else {
			if($wcService->ok()) {
				$wcService->loadExternal();
				if($wcService->check()) {
					$wcService->run();
				}
			}

			$wcService->prompt();
		}
	} else {
		$srv = new WCServiceDummy();
		$srv->setError(WC_HTTPERROR_BAD_REQUEST, "Wrong parameters");
		$srv->prompt();
	}

	$timer->saveGlobal($cacheId);
} else {
	$srv = new WCServiceDummy();
	$srv->setError(WC_HTTPERROR_INTERNAL_SERVER_ERROR, "System is not installed");
	$srv->prompt();
}
